package com.woniuxy.yoga.progress.web.controller;


import cn.hutool.core.bean.BeanUtil;
import com.woniuxy.yoga.commons.core.model.PageInfo;
import com.woniuxy.yoga.commons.core.model.Result;
import com.woniuxy.yoga.commons.core.util.JWTUtil;
import com.woniuxy.yoga.commons.web.log.YogaLog;
import com.woniuxy.yoga.progress.model.form.SelectVacateForm;
import com.woniuxy.yoga.progress.model.form.VacateForm;
import com.woniuxy.yoga.progress.model.param.SelectVacateParam;
import com.woniuxy.yoga.progress.model.param.VacateParam;
import com.woniuxy.yoga.progress.service.TeamVacateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

/**
 * <p>
 * 团课请假记录表 前端控制器
 * </p>
 *
 * @author 韦用文,刘存益,郎腾霄
 * @since 2023年11月28日
 */
@RestController
@RequestMapping("/teamVacate")
@Api(tags = "请假接口管理")
@Validated
public class TeamVacateController {

    @Autowired
    private TeamVacateService teamVacateService;

    /**
     * 用户请假申请
     * @param form 参数
     * @param authen_token 令牌
     * @return string
     */
    @PostMapping("/vacate.us")
    @ApiOperation("用户请假申请")
    @YogaLog("用户请假申请")
    public Result<String> vacate(@RequestBody @Valid VacateForm form, @RequestHeader String authen_token){
        Integer userId = JWTUtil.getUserId(authen_token);
        VacateParam param = BeanUtil.toBean(form, VacateParam.class);
        param.setUserId(userId);
        teamVacateService.vacate(param);
        return Result.getResult(String.class).setMessage("申请成功");
    }

    /**
     * 同意请假
     * @param vacateId 请假表id
     */
    @GetMapping("/agreeVacate/{vacateId}")
    @ApiOperation("同意请假")
    @YogaLog("同意请假")
    public Result<?> agreeVacate(@PathVariable @Valid @NotNull Integer vacateId){
        teamVacateService.agreeVacate(vacateId);
        return Result.getResult().setMessage("操作成功");
    }

    /**
     * 拒绝请假
     * @param vacateId 请假表id
     */
    @GetMapping("/rejectVacate/{vacateId}")
    @ApiOperation("拒绝请假")
    @YogaLog("拒绝请假")
    public Result<?> rejectVacate(@PathVariable @Valid @NotNull Integer vacateId){
        teamVacateService.rejectVacate(vacateId);
        return Result.getResult().setMessage("操作成功");
    }

    /**
     * 分页查询请假申请
     * @return PageInfo
     */
    @PostMapping("/selectVacates")
    @ApiOperation("分页查询请假申请")
    @YogaLog("分页查询请假申请")
    public Result<PageInfo> selectVacates(@RequestBody SelectVacateForm form){
        PageInfo pageInfo = teamVacateService.selectVacates(BeanUtil.toBean(form, SelectVacateParam.class));
        return Result.getResult(PageInfo.class).setMessage("查询成功").setData(pageInfo);
    }
}

